<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/location.php';

class LocationDao {
	private $_db;

	public function __construct() {
		$this->_db = new DbConnection();
		$this->_db->connect();
	}

	/**
	 *
	 * Get all the locations having a specific parentID
	 */
	public function getLocationList($parentID){
		//create query string to get all the user name
		if ($parentID != 0) {
			$query = sprintf("SELECT * FROM location where Parent = '%u'", $parentID);
		} else {
			$query = "SELECT * FROM location where Parent is NULL";
		}
		$result = $this->_db->executeQuery($query);

		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		$locationList = Array();
		
		while($row = mysql_fetch_assoc($result)) {
			$locationList[] = $this->setLocationByAssoc($row);			
		}		
		
		mysql_free_result($result);
		return $locationList;
	}
	
	public function getLocationFromID($locationID) {
		$query = sprintf("SELECT * FROM location where LocationID = '%u'", $locationID);
		
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if ($row = mysql_fetch_assoc($result)) {			
			$location = $this->setLocationByAssoc($row);					
		}
		
		mysql_free_result($result);
		
		return $location;
	}
	
	public function getLocationByRegion($region) {
		$query = sprintf("SELECT * FROM location where NAME LIKE '%s'", '%' . $region . '%');
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if ($row = mysql_fetch_assoc($result)) {			
			$location = $this->setLocationByAssoc($row);					
		}
		
		mysql_free_result($result);
		
		return $location;
	}
	
	public function getLocationNearest($latitude, $longitude) {
		$query = sprintf("SELECT *, (f_calSqrDistanceTwoPoints( %f, %f, Latitude, Longitude )) AS Distance
					FROM location
					WHERE Parent IS NULL
					ORDER BY Distance ASC
					LIMIT 0 , 1", $latitude, $longitude);
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if ($row = mysql_fetch_assoc($result)) {			
			$location = $this->setLocationByAssoc($row);					
		}
		
		mysql_free_result($result);
		
		return $location;
	}

	public function getLocationByRegionAndParent($region, $parent, $type) {
		$query = sprintf("SELECT * FROM location WHERE LCASE(name) LIKE '%s' AND type = '%d'", '%' . $region . '%', $type);
		if ($parent == null) {
			$query .= "AND Parent IS NULL";
		} else if (isset($parent)){
			$query .= sprintf("AND Parent = %d", $parent);
		}
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if ($row = mysql_fetch_assoc($result)) {			
			$location = $this->setLocationByAssoc($row);
			return $location;		
		}
		
		mysql_free_result($result);
	}
	
	private function setLocationByAssoc($row) {
		$location = new LocationModel();
			
		$location->setLocationID($row['LocationId']);
		$location->setParent($row['Parent']);
		$location->setName($row['Name']);
		$location->setType($row['Type']);
		$location->setLatitude($row['Latitude']);
		$location->setLongitude($row['Longitude']);
		
		return $location;
	}
}
?>